package cn.framelife.rainbow.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.internal.Workbench;

/**
 * 项目工具类
 *
 * @ClassName: ProjectUtil 
 * @author 潘广伟  p_3er@qq.com 
 * @date 2013年11月22日 下午3:03:30 
 *
 */
public class ProjectUtil {
	/**
	 * 获取当前的项目
	 *
	 * @Title: getCurrentProject 
	 * @param @return  
	 * @return IProject  
	 * @throws
	 */
	public static IProject getCurrentProject(){    
        ISelectionService selectionService =     
            Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();    
    
        ISelection selection = selectionService.getSelection();    
    
        IProject project = null;    
        if(selection instanceof IStructuredSelection) {    
            Object element = ((IStructuredSelection)selection).getFirstElement();    
    
            if (element instanceof IResource) {    
                project= ((IResource)element).getProject();    
            } else if (element instanceof PackageFragmentRootContainer) {    
                IJavaProject jProject =     
                    ((PackageFragmentRootContainer)element).getJavaProject();    
                project = jProject.getProject();    
            } else if (element instanceof IJavaElement) {    
                IJavaProject jProject= ((IJavaElement)element).getJavaProject();    
                project = jProject.getProject();    
            }    
        }     
        return project;    
    } 
	
	/**
	 * 获取 Workspace下所有的Web项目
	 *
	 * @param @return
	 * @param @throws Exception    设定文件 
	 * @return IProject[]    返回类型 
	 * @throws
	 */
	public static IProject[] getWebProjects() throws Exception{
		IProject[] projects = getProjects();
		List<IProject> list = new ArrayList<IProject>();
		for(IProject project : projects){
			if(project.getDescription().hasNature("org.eclipse.wst.common.project.facet.core.nature")){
				list.add(project);
			}
		}
		projects = new IProject[list.size()];
		
		for(int i=0; i < list.size(); i++){
			projects[i] = list.get(i);
		}
		
		return projects;
	}
	
	/**
	 * 获取Workspace下所有的项目
	 *
	 * @Title: getPojects 
	 * @param @return  
	 * @return IProject[]  
	 * @throws
	 */
	public static IProject[] getProjects(){
		return ResourcesPlugin.getWorkspace().getRoot().getProjects();
	}
	
	/**
	 * 获取选定的项目
	 *
	 * @Title: getSelectionProject 
	 * @param @param projectName
	 * @param @return  
	 * @return IProject  
	 * @throws
	 */
	public static IProject getSelectionProject(String projectName){
		IProject selectionProject = null;
		for(IProject project : ProjectUtil.getProjects()){
			if(project.getName().equals(projectName)){
				selectionProject = project;
			}
		}
		return selectionProject;
	}
	
	/**
	 * 获取项目的路径
	 *
	 * @Title: getProjectPath 
	 * @param @param projectName
	 * @param @return  
	 * @return String  
	 * @throws
	 */
	public static String getProjectPath(String projectName){
		return ProjectUtil.getSelectionProject(projectName).getLocation().toOSString();
	}
	
	/**
	 * 刷新工程并编译源码
	 * 
	 * @Title: refreshProject 
	 * @param @param proectName
	 * @param @throws Exception  
	 * @return void     
	 * @throws
	 */
	public static void refreshProject(String proectName) throws Exception{
		IProject selectionProject = getSelectionProject(proectName);	
		selectionProject.refreshLocal(IResource.DEPTH_INFINITE, null);
	}
}
